package it.gtd.swimpool.model.entity;

import com.google.common.annotations.Beta;

import javax.persistence.*;
import java.util.Date;

import static com.google.common.base.Preconditions.checkNotNull;

@Entity
@Table(name = "friendship_requests")
@Beta
@NamedQueries({
    @NamedQuery(name = "FriendshipRequest.byTargetId",
        query = "SELECT fr FROM FriendshipRequest AS fr WHERE fr.composedId.target.id = :id"),
    @NamedQuery(name = "FriendshipRequest.bySenderTargetIds",
        query = "SELECT fr FROM FriendshipRequest AS fr WHERE fr.composedId.target.id = :targetId AND fr.composedId.sender.id = :senderId")
})
public class FriendshipRequest {
    @EmbeddedId
    private FriendshipRequestId composedId;

    @Basic
    private Date date;

    public FriendshipRequest() {
        this.date = new Date();

        this.composedId = composedId == null ? new FriendshipRequestId() : this.composedId;
    }

    public FriendshipRequest(User sender, User target) {
        this();

        checkNotNull(sender, "sender can't be null");
        checkNotNull(target, "target can't be null");

        this.composedId.setSender(sender);
        this.composedId.setTarget(target);
    }

    public User getSender() {
        return this.composedId.getSender();
    }

    public User getTarget() {
        return this.composedId.getTarget();
    }


    public Date getDate() {
        return date;
    }
}
